﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Assesment.Library;
using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DomainObject.Transaction;
using Training.Library.DomainObject.Administration.Catalog;

namespace Assesment.Library.DataAccess.Transaction
{
    public partial class AssesmentPositionDetailRepo
    {
        public List<AssesmentPositionDetail> GetListByAssesmentPositionID(int AssesmentPositionID, string CompetencyDesc)
        {
            List<AssesmentPositionDetail> tt = new List<AssesmentPositionDetail>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ManagerialDetailID, ManagerialKeyBehaviourID, KeyBehaviourDetailID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.ManagerialKeyBehaviourDetail";
                    cmd.CommandText = "SP_AssesmentPositionDetail_SelectByAssesmentPositionID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@AssesmentPositionID", SqlDbType.Int).Value = AssesmentPositionID;
                    cmd.Parameters.Add("@CompetencyDesc", SqlDbType.VarChar).Value = CompetencyDesc;

                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {

                                AssesmentPositionDetail t = new AssesmentPositionDetail(true);
                                t.AssesmentPositionDtlID = dr[AssesmentPositionDetail.Fields.AssesmentPositionDtlID] == DBNull.Value ? 0 : int.Parse(dr[AssesmentPositionDetail.Fields.AssesmentPositionDtlID].ToString());
                                t.AssesmentPositionID = dr[AssesmentPositionDetail.Fields.AssesmentPositionID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionDetail.Fields.AssesmentPositionID].ToString());
                                t.CompetencyID = dr[AssesmentPositionDetail.Fields.CompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionDetail.Fields.CompetencyID].ToString());
                                t.KeyBehaviourDetailID = dr[AssesmentPositionDetail.Fields.KeyBehaviourDetailID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionDetail.Fields.KeyBehaviourDetailID].ToString());
                                t.StatusKeyBehaviour = dr[AssesmentPositionDetail.Fields.StatusKeyBehaviour] == DBNull.Value ? (bool?)null : bool.Parse(dr[AssesmentPositionDetail.Fields.StatusKeyBehaviour].ToString());
                                t.Rating = dr[AssesmentPositionDetail.Fields.Rating] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionDetail.Fields.Rating].ToString());
                                t.CreatedDate = dr[AssesmentPositionDetail.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[AssesmentPositionDetail.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[AssesmentPositionDetail.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[AssesmentPositionDetail.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[AssesmentPositionDetail.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[AssesmentPositionDetail.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[AssesmentPositionDetail.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[AssesmentPositionDetail.Fields.UpdatedBy].ToString();

                                t.KeyBehaviourDetail = new KeyBehaviourDetail(true);
                                t.KeyBehaviourDetail.KeyBehaviourName = dr[KeyBehaviourDetail.Fields.KeyBehaviourName] == DBNull.Value ? string.Empty : dr[KeyBehaviourDetail.Fields.KeyBehaviourName].ToString();
                                t.KeyBehaviourDetail.LevelCompetency.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                tt.Add(t);
                                t.Dispose();


                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		
    }
}
